package com.vitogiovanni.tobook;
import java.util.LinkedList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.widget.DrawerLayout;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.FrameLayout;
import android.widget.ListView;

import com.vitogiovanni.tobook.utilities.NavigationItem;
import com.vitogiovanni.tobook.utilities.NavigationItemAdapter;
import com.vitogiovanni.tobook.utilities.NetworkChecker;
import com.vitogiovanni.tobook.utilities.UserEmailFetcher;

public class BaseActivity extends Activity
{
	protected List<NavigationItem> itemList;
    protected DrawerLayout mDrawerLayout;
    private ListView mDrawerList;
    
    private ActionBarDrawerToggle mDrawerToggle;
    protected CharSequence mDrawerTitle;
    protected CharSequence mTitle;
    
    protected FrameLayout actContent;
    
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
    	super.onCreate(savedInstanceState);
        super.setContentView(R.layout.activity_base);
        
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawerList = (ListView) findViewById(R.id.left_drawer);

        // Set the adapter for the list view
        Resources res = getResources();
        itemList = new LinkedList<NavigationItem>();
        itemList.add(new NavigationItem("Qui Vicino", res.getDrawable(R.drawable.location_gps)));
        itemList.add(new NavigationItem("Ricerca", res.getDrawable(R.drawable.ricerca_locale)));
        itemList.add(new NavigationItem("Preferiti", res.getDrawable(R.drawable.preferiti)));
        itemList.add(new NavigationItem("Prenotazioni", res.getDrawable(R.drawable.storico)));
        NavigationItemAdapter adapter = new NavigationItemAdapter(this, R.layout.drawer_list_item, itemList.toArray(new NavigationItem[0]));
        mDrawerList.setAdapter(adapter);
        // Set the list's click listener
        
        mDrawerList.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				handleClick(position);
			}

			private void handleClick(int position) {
				String function = itemList.get(position).getName();
				if(function.equals("Qui Vicino"))
					{
					if (!NetworkChecker.isInternetAvailable(BaseActivity.this)) {
						NetworkChecker.noConnectionToast(BaseActivity.this);
						return;
					}
					Intent intent = new Intent(BaseActivity.this, ListaLocaliActivity.class);
					startActivity(intent);
					}
				else if(function.equals("Ricerca"))
				{
					if (!NetworkChecker.isInternetAvailable(BaseActivity.this)) {
						NetworkChecker.noConnectionToast(BaseActivity.this);
						return;
					}
					Intent intent = new Intent(BaseActivity.this, RicercaLocaliActivity.class);
					startActivity(intent);
				}
				else if(function.equals("Preferiti"))
				{
					if (!NetworkChecker.isInternetAvailable(BaseActivity.this)) {
						NetworkChecker.noConnectionToast(BaseActivity.this);
						return;
					}
					Context context = getApplicationContext();
					
					CharSequence text = UserEmailFetcher.getEmail(context);
					Intent intent = new Intent(BaseActivity.this, PreferitiActivity.class);
					intent.putExtra(MainActivity.EMAIL_UTENTE, text.toString());
					startActivity(intent);
				}
				else if(function.equals("Prenotazioni"))
				{
					if (!NetworkChecker.isInternetAvailable(BaseActivity.this)) {
						NetworkChecker.noConnectionToast(BaseActivity.this);
						return;
					}
					Context context = getApplicationContext();
					
					CharSequence text = UserEmailFetcher.getEmail(context);
					Intent intent = new Intent(BaseActivity.this, ListaPrenotazioniActivity.class);
					intent.putExtra(MainActivity.EMAIL_UTENTE, text.toString());
					startActivity(intent);
				}
			}
		});
        
        mTitle = mDrawerTitle = getTitle();
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
                R.drawable.ic_drawer, R.string.ok, R.string.no) {

            /** Called when a drawer has settled in a completely closed state. */
            public void onDrawerClosed(View view) {
                super.onDrawerClosed(view);
                getActionBar().setTitle(mTitle);
                invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
            }

            /** Called when a drawer has settled in a completely open state. */
            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);
                getActionBar().setTitle(mDrawerTitle);
                invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
            }
        };

        // Set the drawer toggle as the DrawerListener
        mDrawerLayout.setDrawerListener(mDrawerToggle);
        
        getActionBar().setDisplayHomeAsUpEnabled(true);
        getActionBar().setHomeButtonEnabled(true);
        
    }
    
    @Override
    public void setContentView(final int layoutResID) {
        actContent= (FrameLayout) mDrawerLayout.findViewById(R.id.content_frame);
        getLayoutInflater().inflate(layoutResID, actContent, true); // Setting the content of layout your provided to the act_content frame
        super.setContentView(mDrawerLayout);
        // here you can get your drawer buttons and define how they should behave and what must they do, so you won't be needing to repeat it in every activity class
    }
    
    /* Called whenever we call invalidateOptionsMenu() *//*
    @Override
    public boolean onPrepareOptionsMenu(android.view.Menu menu) {
        // If the nav drawer is open, hide action items related to the content view
        boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
        menu.findItem(R.id.action_settings).setVisible(!drawerOpen);
        return super.onPrepareOptionsMenu(menu);
    }*/
    
    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        // Sync the toggle state after onRestoreInstanceState has occurred.
        mDrawerToggle.syncState();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        mDrawerToggle.onConfigurationChanged(newConfig);
    }

    @Override
    public boolean onOptionsItemSelected(android.view.MenuItem item) {
        // Pass the event to ActionBarDrawerToggle, if it returns
        // true, then it has handled the app icon touch event
        if (mDrawerToggle.onOptionsItemSelected(item)) {
          return true;
        }
        // Handle your other action bar items...

        return super.onOptionsItemSelected(item);
    }

}